<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAgentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('agents', function (Blueprint $table) {
            $table->id();
            $table->string('name')->comment('代理名称');
            $table->char('phone', 16)->unique()->comment('手机号'); // 16 位原有系统因素
            $table->string('password')->comment('密码');
            $table->tinyInteger('level')->default(1)->comment('代理等级; 1:群主 2:精英 3:大咖');
            $table->tinyInteger('cultivation_award')->default(1)->comment('培养奖; 1:开启 2:关闭');

            $table->string('real_name')->default('')->comment('代理姓名');
            $table->tinyInteger('gender')->default(1)->comment('性别; 1:男 2:女');
            $table->string('wechat_number')->default('')->comment('微信号');
            $table->date('birthday')->nullable()->comment('生日');

            $table->char('province', 16)->default('')->comment('省');
            $table->char('city', 16)->default('')->comment('市');
            $table->char('area', 16)->default('')->comment('区');
            $table->string('address')->default('')->comment('收货地址');

            // 业务需要出现负值
            $table->bigInteger('balance')->default(0)->comment('可提现余额 单位(分)');
            $table->unsignedBigInteger('amount_in_withdrawal')->default(0)->comment('提现(中)金额 单位(分)');
            $table->bigInteger('unextractable_balance')->default(0)->comment('不可提现余额 单位(分)');

            $table->unsignedBigInteger('reward')->default(0)->comment('累计获得奖励');
            $table->unsignedBigInteger('income')->default(0)->comment('累计获得收入');
            $table->unsignedBigInteger('commission')->default(0)->comment('累计获得佣金');
            $table->unsignedBigInteger('personal_performance')->default(0)->comment('个人业绩 单位(分)');
            $table->unsignedInteger('number_of_personal_order')->default(0)->comment('个人订单数');

            $table->unsignedBigInteger('team_reward')->default(0)->comment('团队累计获得奖励');
            $table->unsignedBigInteger('team_income')->default(0)->comment('团队累计获得收入');
            $table->unsignedBigInteger('team_commission')->default(0)->comment('团队累计获得佣金');
            $table->unsignedBigInteger('team_performance')->default(0)->comment('团队业绩 单位(分)');
            $table->unsignedInteger('number_of_team_order')->default(0)->comment('团队订单数');

            $table->bigInteger('direct_inviter')->index()->default(0)->comment('用户的直接邀请人');
            $table->unsignedInteger('number_of_direct_inviter')->default(0)->comment('当前用户直接邀请了多少人');
            $table->bigInteger('indirect_inviter')->index()->default(0)->comment('用户的间接邀请人');
            $table->unsignedInteger('number_of_indirect_inviter')->default(0)->comment('当前用户间接邀请了多少人');

            $table->bigInteger('affiliated_partner')->index()->default(0)->comment('所属合伙人');
            $table->text('parent_path_id')->nullable()->comment('上级邀请人ID路径,减号相隔,上级到下级从前到后');
            $table->char('invitation_code', 6)->unique()->comment('邀请代码');

            $table->integer('login_frequency')->default(0)->comment('登录次数');
            $table->timestamp('last_time_of_login')->nullable()->comment('最后登录时间');

            $table->boolean('is_agree_user_agreement')->default(false)->comment('是否已经同意用户协议;用在用户是否首次登录,去微信授权');
            $table->tinyInteger('status')->default(1)->comment('状态; 1:正常 2:禁用');

            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('agents');
    }
}
